package com.lin.controller;

import com.lin.annotation.MyLog;
import com.lin.pojo.Wares;
import com.lin.pojo.Order;
import com.lin.pojo.WaresOrder;
import com.lin.services.ReduceOrderService;
import com.lin.utils.PageUnit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * ClassName ReduceOrderController
 * Package com.lin.controller
 *
 * @author hellcat
 * Date 2020/3/14
 * Description:退货单管理
 * 🐾
 */
@Controller
@RequestMapping("/reduceOrder")
public class ReduceOrderController {

    @Autowired
    private ReduceOrderService reduceOrderService;

    @MyLog("访问退货单")
    @RequiresPermissions("purchase:reduceOrder:list")
    @RequestMapping(value = "/toReduceOrder", method = RequestMethod.GET)
    public String toReduceOrder(HttpServletRequest request) {
        request.setAttribute("ddId", "ReduceOrder");
        return "reduceOrderMain";
    }

    @RequiresPermissions("purchase:reduceOrder:list")
    @ResponseBody
    @RequestMapping(value = "/getReduceOrder", method = RequestMethod.POST)
    public Map<String, Object> getReduceOrder(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String searchOrderId = request.getParameter("searchOrderId");
        String searchDate = request.getParameter("searchDate");
        List<Order> list = reduceOrderService.getReduceOrder(searchDate, searchOrderId);
        return PageUnit.Page(list, page, limit);
    }

    @RequiresPermissions("purchase:reduceOrder:insert")
    @RequestMapping(value = "/toInsertReduceOrder", method = RequestMethod.GET)
    public String toInsertReduceOrder(HttpServletRequest request) {
        request.setAttribute("ddId", "ReduceOrder");
        return "reduceOrderAdd";
    }

    @RequiresPermissions("purchase:reduceOrder:insert")
    @ResponseBody
    @RequestMapping(value = "/getAllWares", method = RequestMethod.POST)
    public Map<String, Object> getAllWares(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String id = request.getParameter("id");
        List<Wares> list = reduceOrderService.getAllWares(id);
        return PageUnit.Page(list, page, limit);
    }

    @MyLog("添加退货单")
    @RequiresPermissions("purchase:reduceOrder:insert")
    @ResponseBody
    @RequestMapping(value = "/insertReduceOrder", method = RequestMethod.POST)
    public Map<String, Object> insertReduceOrder(@RequestBody List<WaresOrder> tableData, HttpServletRequest request) {
        return reduceOrderService.insertReduceOrder(tableData);
    }

    @MyLog("查看退货单单详情")
    @RequiresPermissions("purchase:reduceOrder:list")
    @RequestMapping(value = "/toReduceOrderDetails", method = RequestMethod.GET)
    public String toDetailsReduceOrder(HttpServletRequest request) {
        request.setAttribute("ddId", "ReduceOrder");
        String id = request.getParameter("orderId");
        if (id != null && !"".equals(id.trim())) {
            Order order = reduceOrderService.getOrderById(id);
            request.setAttribute("order", order);
            return "reduceOrderDetails";
        } else {
            return "redirect:/reduceOrder/toReduceOrder";
        }
    }

    @RequiresPermissions("purchase:reduceOrder:list")
    @ResponseBody
    @RequestMapping(value = "/getOrderWaresByOrderId", method = RequestMethod.POST)
    public Map<String, Object> getOrderWaresByOrderId(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        String id = request.getParameter("id");
        int limit = Integer.parseInt(request.getParameter("limit"));
        List<WaresOrder> list = reduceOrderService.getOrderWaresByOrderId(id);
        return PageUnit.Page(list, page, limit);
    }
}
